<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/21
 * Time: 13:25
 */

namespace app\common\mysic\user;


use app\common\mysic\MysicBase;
use app\common\model\dealer\Capital;
use app\common\model\dealer\User;
use think\Db;
use think\Model;

class DealUserAccountOp extends MysicBase
{
    /**
     * @var User
     */
    protected $dealUserEntity;
    /**
     * @var Capital
     */
    protected $dealUserCapitalModel;
    /**
     * @var float
     */
    protected $bonus;
    public function setUserEntity(Model $entity)
    {
        $this->dealUserEntity = $entity;
        return $this;
    }

    public function setUserCapitalModel(Model $model)
    {
        $this->dealUserCapitalModel = $model;
        return $this;
    }

    public function setBonus($bonus)
    {
        $this->bonus = $bonus;
        return $this;
    }

    public function transfer()
    {
        Db::transaction(function(){
            $this->dealUserEntity->money = $this->dealUserEntity->money + $this->bonus;
            $this->dealUserEntity->isUpdate(true)->save();

            $this->dealUserCapitalModel->data([
                'wxapp_id'  => $this->dealUserEntity->wxapp_id,
                'user_id'   => $this->dealUserEntity->user_id,
                'flow_type' => 30,
                'money'     => $this->bonus,
                'describe'  => '分红转换'
            ])->isUpdate(false)->save();

        });
        return true;
    }

}